package com.developer.platform.controller.data;

import com.developer.platform.application.command.NoticeCmdManager;
import com.developer.platform.application.command.cmd.NoticeAdd;
import com.developer.platform.application.command.cmd.NoticeUpdate;
import com.developer.platform.application.query.NoticeQueryManager;
import com.developer.platform.application.query.dto.NoticeListDto;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.controller.BaseController;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import com.veeker.log.annotations.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2020-11-10
 */
@Api(tags = "更新通知管理")
@Validated
@RequestMapping("/data/notice")
@RestController
public class NoticeController extends BaseController {

    private final NoticeQueryManager noticeQueryManager;
    private final NoticeCmdManager noticeCmdManager;

    public NoticeController(NoticeQueryManager noticeQueryManager,
                            NoticeCmdManager noticeCmdManager) {
        this.noticeQueryManager = noticeQueryManager;
        this.noticeCmdManager = noticeCmdManager;
    }


    @ApiImplicitParams({
            @ApiImplicitParam(value = "群ID", name = "groupId", required = false),
            @ApiImplicitParam(value = "环境", name = "templateId", required = false),
            @ApiImplicitParam(value = "状态", name = "status", required = false),
            @ApiImplicitParam(value = "更新人员ID", name = "userIds", required = false),
            @ApiImplicitParam(value = "页大小", name = "pageSize", required = true),
            @ApiImplicitParam(value = "当前页", name = "pageNumber", required = true),
    })
    @Log(businessType = "更新通知管理",discription = "查询更新通知列表")
    @ApiOperation(value = "查询更新通知列表")
    @GetMapping
    public ResponseEntity<ResponseModel<NoticeListDto>> page(@RequestParam(value = "groupId" , required = false) Long groupId,
                                                             @RequestParam(value = "templateId" , required = false) Long templateId,
                                                             @RequestParam(value = "userIds" , required = false) List<Long> userIds,
                                                             @RequestParam(value = "status" , required = false) StatusEnum status,
                                                             Pageable pageable){
        return ok(noticeQueryManager.page(groupId, templateId, userIds, status, pageable));
    }

    @ApiOperation(value = "查询更新通知总数")
    @GetMapping("/count")
    public ResponseEntity<Integer> count(){
        return ok(noticeQueryManager.count());
    }

    @Log(businessType = "更新通知管理",discription = "新增更新通知信息")
    @ApiOperation(value = "新增更新通知信息")
    @PostMapping
    public ResponseEntity<?> save(@Valid @RequestBody NoticeAdd noticeAdd){
        noticeCmdManager.save(noticeAdd);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @Log(businessType = "更新通知管理",discription = "修改更新通知信息")
    @ApiOperation(value = "修改更新通知信息")
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable("id") long id ,
                                  @Valid @RequestBody NoticeUpdate noticeUpdate){
        noticeCmdManager.update(id, noticeUpdate);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @Log(businessType = "更新通知管理",discription = "删除更新通知信息")
    @ApiOperation(value = "删除更新通知信息")
    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") long id){
        noticeCmdManager.delete(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
